CmdUtils.CreateCommand({
  name: "tiny-geocode",
  icon: "http://tinygeocoder.com/favicon.ico",
  homepage: "http://tinygeocoder.com/",
  author: { name: "Mskadu", email: "mskadu@gmail.com"},
  license: "GPL",
  description: "Turns a location (E.g. London, UK) into a latitude longitude pair (e.g. 51.5001524,-0.1262362)",
  help: "tiny-geocode <location><BR/>Eg: tiny-geocode London, UK",
  takes: {"location": noun_arb_text},
  tinyGeoCode: function( location )
  {
    var remoteUrl = "http://tinygeocoder.com/create-api.php?q=" + location;
    var retVal;
    var ajaxOptions = {
        url: remoteUrl, 
        type: "GET",
        datatype: "string",
        async: false /*,
        success: function(responseText) {            
          this.retVal = responseText;
        },
        error: function() {
           displayMessage("error");
        } */
     };
    
    retVal = jQuery.ajax(ajaxOptions).responseText;
    return( retVal );
   
  },
  preview: function( pblock, input ) {
    if( input.text )
    {
    //to be changed
    var API_KEY = "ABQIAAAAO0oNFUXoUNx4MuxcPwakNhR3yUCx-o6JvWtDFa7jNOakHN7MrBSTsaKtGJjaVMeVURIpTa3cD1qNfA"; 
     
    var msg = "<b>Location</b>: ${location}<BR/>" +
              '<img src="${mapimg}" alt="map" /><BR/>' +
              "<b>Geocode</b>: ${geocode}<BR/><BR/>" +
              "Press Enter to copy it to the clipboard";
    var geoCode = this.tinyGeoCode( input.text );   
    var mapURL = "http://maps.google.com/staticmap?center=" + geoCode +
                 "&span=1,1&size=500x300&markers=blues|" + geoCode +
                 "&key=" + API_KEY;
    pblock.innerHTML = CmdUtils.renderTemplate( msg,
                                                {location: input.text,
                                                 mapimg: mapURL,
                                                 geocode: geoCode,
                                                }
                                                );
    }
   
  },
  execute: function(input) {
         var geoCode = this.tinyGeoCode( input.text );
         CmdUtils.copyToClipboard( geoCode );
         displayMessage("Geocode " + geoCode + " copied to the clipboard" );   
  }
});

